<?php

namespace app\help;

use phpseclib3\Crypt\AES;


class EyEncryptHelper
{
    private static string $publicKey = "-----BEGIN PUBLIC KEY-----\n" .
    "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCEqv2TMxj/EgXmCO9zbyiIA/omXprrrmZhudN237lQ3qWSsxaPZlU0c79PtEEEc8nW/wAPkzUIiuOWX4N9SebZYBh+7+VmeFuyjNvMKOFTxdoQdVlWk4u8k3Jm4OBigH4YgtcSrU+LmZLQ7DD5zp9IBxWpUVewVrYyehfUHx3ppwIDAQAB\n" .
    "-----END PUBLIC KEY-----";


    private static string $res_iv = 'wt#f7iZbHC*j5Z7F';
    private static string $res_private_key = '8yoHY1JYh7/D2yTxG27ocw==';



    public static function encrypt_query($query_data): string
    {
        $data = json_encode($query_data);
        $encrypt_data = '';

        // 将数据分段加密
        $chunks = str_split($data, 100); // 每块100字节
        foreach ($chunks as $chunk) {
            // 使用公钥进行加密
            openssl_public_encrypt($chunk, $encrypted_chunk, self::$publicKey);
            $encrypt_data .= $encrypted_chunk; // 拼接加密后的数据
        }

        // base64编码
        return base64_encode($encrypt_data);
    }


   public static function decryption_result($encrypt_data): string
   {
       $aes = new AES('cbc');
       $aes->setKey(self::$res_private_key);
       $aes->setIV(self::$res_iv);
       $ciphertext = base64_decode($encrypt_data);
       // AES解密
       return $aes->decrypt($ciphertext);
    }

}